import * as api from '@/services/api';
import list from '@/components/data_list/list';

export default {
  namespace: 'list',
  state: {
    listData: [],
    page: 1,
    totalPage: 1,
    isLoading: false,
    hasMore: true,
    dataSource: [],
  },
  effects: {
    // @ts-ignore
    *getList(param: any, { call, put }) {
      const data = yield call(api.getList, param.payload);
      yield put({
        type: 'add',
        data: data.data,
      });
    },
  },
  reducers: {
    add(state: any, action: any) {
      const { listData, page, totalPage } = action.data;
      let tempList = state.dataSource.concat(listData);
      return {
        ...state,
        listData: listData,
        dataSource: tempList,
        page: page,
        totalPage: totalPage,
      };
    },
    resetSource(state: any) {
      state.dataSource = [];
      state.listData = [];
      return { ...state };
    },
  },
};
